// pages/monthReportEdit/monthReportEdit.js
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    monthCollocation:[],
    now_month_text:'',
    now_year:'',
    now_month:'',
    info:{},
  },

/**切换年份 */
changeMonth(e){
  let type = e.currentTarget.dataset.type;
  let month = '';
  let year = this.data.now_year
  console.log(this.data.now_year);
  if(type=='pre'){
    month=(Number(year)-1)+'-12';
  }else if(type=='next'){
    month=(Number(year)+1)+'-01';
  }
  //请求数据
  this.requstData(month,'')
},
  /**
   * 月份点击事件
   */
  monthClick:function(e){
    this.requstData(e.currentTarget.dataset.month)
  },
 
 

  /**上传文件 */
  afterRead(event) {
    let that = this;
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式

    api.uploadFileCRM(app.root+'?cla=fileUploadApp&fun=uploadFile',file,{power: 'workReportMonth',fileType:'月报'}).then((res)=>{
      // 上传完成需要更新 fileList
           const { fileList = [] } = that.data;
           fileList.push({ ...file, url: res.result.src,id:res.result.id });
           that.setData({ fileList });
         })
  },
  /**删除文件 */
  delFile(event){
    let index = event.detail.index;
    let fileList = this.data.fileList;
    let newFileList=[];
    let id = '';
    fileList.forEach((v,i)=>{
      if(i==index){
        id=v.id;
      }else{
        newFileList.push(v);
      }
    })
  
    let that = this;
    api.curlPost('?cla=fileUploadApp&fun=delFile',{id:id,power:'workReportMonth'}).then((res)=>{
      that.setData({
        fileList:newFileList
      })
    })
    
  },

  /**提交 */
  commit(e){
    let para = e.detail.value;
    let fileList = this.data.fileList;
    let fileId = [];
    fileList.forEach((v,i)=>{
      fileId.push(v.id)
    })
    para.fileId = JSON.stringify(fileId);
    para.id=!api.isBlank(this.data.info.id)?this.data.info.id:'',
    para.location = this.data.info.now_month+'-01',
    para.type='月报';
    api.curlPost('?cla=workReportApp&fun=commitEdit',para).then((res)=>{
      wx.redirectTo({
        url: '../monthReportInfo/monthReportInfo?id='+res.data.id
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(api.isBlank(options.id)){
      this.requstData()
   } else{
      this.requstData('',options.id)
   }
  },
  requstData(month='',id=''){
    let that = this;
    api.curlPost('?cla=workReportApp&fun=detail',{type:'月报',id:id,month:month}).then((res)=>{
        that.setData({
          info:res.data.result,
          monthCollocation:res.data.result.dayRecord,
          now_month:res.data.result.now_month,
          now_year:res.data.result.now_year,
          now_month_text:api.formatDateCRM('Y年',res.data.result.now_month),
          fileList:res.data.result.attachFile?res.data.result.attachFile:[],
        }) 
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})